/**
 * 验证小数点后两位及多个小数
 * money 金额
 */
export function isMoney(money) {
	var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
	if (reg.test(money)) {
		return true
	} else {
		return false
	}
}

/**
 * 验证手机号码
 * money 金额
 */
export function checkPhone(c2543fff3bfa6f144c2f06a7de6cd10c0b650cae) {
	var reg = /^1(3|4|5|6|7|8|9)\d{9}$/
	if (reg.test(c2543fff3bfa6f144c2f06a7de6cd10c0b650cae)) {
		return true
	} else {
		return false
	}
}

/**
 * 函数防抖 (延迟执行，只执行最后一次点击)
 * @param fn
 * @param delay
 * @returns {Function}
 * @constructor
 */
export const Debounce = (fn, t) => {
	const delay = t || 500
	let timer
	return function() {
		const args = arguments
		if (timer) {
			clearTimeout(timer)
		}
		timer = setTimeout(() => {
			console.log("执行");
			timer = null
			fn.apply(this, args)
		}, delay)
	}
}
/**
 * 函数防抖 (立即执行，只执行最后一次点击)
 * @param fn
 * @param delay
 * @returns {Function}
 * @constructor
 */
export const debounceImmediate = (fn, t) => {
	const delay = t || 500
	let timer

	let immediateExecuted = false;
	return function() {
		const args = arguments
		if (!immediateExecuted) {
			fn.apply(this, args)
			immediateExecuted = true;
		}
		clearTimeout(timer)

		timer = setTimeout(() => {
			immediateExecuted = false
		}, delay)
	}
}
/**
 * 函数节流
 * @param fn
 * @param interval
 * @returns {Function}
 * @constructor
 */
export const Throttle = (fn, t) => {
	let last
	let timer
	const interval = t || 500
	return function() {
		const args = arguments
		const now = +new Date()
		if (last && now - last < interval) {
			clearTimeout(timer)
			timer = setTimeout(() => {
				last = now
				fn.apply(this, args)
			}, interval)
		} else {
			last = now
			fn.apply(this, args)
		}
	}
}

/**
 * app权限授权弹窗
 * @param permissionID
 * @returns {Promise}
 * @constructor
 * @example requestAndroidPermission('android.permission.CAMERA')
 */
export const requestAndroidPermission = (permissionID) => {
	return new Promise((resolve, reject) => {
		plus.android.requestPermissions(
			[permissionID],
			(resultObj) => {
				let result = 1;
				for (var i = 0; i < resultObj.granted.length; i++) {
					var grantedPermission = resultObj.granted[i];
					result = 1
					console.log('已获取的权限：' + result);
				}
				for (var i = 0; i < resultObj.deniedPresent.length; i++) {
					var deniedPresentPermission = resultObj.deniedPresent[i];
					result = 0
					console.log('拒绝本次申请的权限：' + result);
				}
				for (var i = 0; i < resultObj.deniedAlways.length; i++) {
					var deniedAlwaysPermission = resultObj.deniedAlways[i];
					result = -1
					console.log('永久拒绝申请的权限：' + result);
				}
				resolve(result);
				// 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
			},
			function(error) {
				console.log('申请权限错误：' + error.code + " = " + error.message);
				resolve({
					code: error.code,
					message: error.message
				});
			}
		);
	})
}